/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QBlog.
 * QBlog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QBlog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QBlog.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QBlog的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QBlog是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QBlog的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

package name.huliqing.qblog.processor.impl;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import name.huliqing.qblog.QBlog;
import name.huliqing.qblog.entity.FolderEn;
import name.huliqing.qblog.entity.PhotoEn;
import name.huliqing.qblog.enums.Security;
import name.huliqing.qblog.service.FolderSe;
import name.huliqing.qblog.service.PhotoSe;
import name.huliqing.qfaces.model.PageModel;
import name.huliqing.qfaces.model.PageParam;

/**
 *
 * @author huliqing
 */
@ManagedBean
@RequestScoped
public class PhotoFetch implements java.io.Serializable {

    // 来自URL中的pageId
    private Long pageId;

    // 来自URL中的folderId
    private Long folderId;

    // 是否已经设置过关于FetchCode的警告窗口，在一个页面存在多个图片集时可能会出现访
    // 问PROTECTED类型的相册时弹出多个警告窗口。这需要避免
    private boolean hasWarnForFetchCode;
    
    public Long getFolderId() {
        return folderId;
    }

    public void setFolderId(Long folderId) {
        this.folderId = folderId;
    }

    public Long getPageId() {
        return pageId;
    }

    public void setPageId(Long pageId) {
        this.pageId = pageId;
    }

    public boolean isHasWarnForFetchCode() {
        return hasWarnForFetchCode;
    }

    public void setHasWarnForFetchCode(boolean hasWarnForFetchCode) {
        this.hasWarnForFetchCode = hasWarnForFetchCode;
    }

    // ---- Load data

    public PageModel<PhotoEn> loadData(PageParam pp) {
        PhotoDataTable t = (PhotoDataTable) pp.getUiData();
        pp.setSortField(t.getSortField());
        pp.setAsc(t.getAsc());
        // 不要直接用folderId,folderId在submit()的时候还需要用到，所以这里不能改变它的值
        Long tempId = null;
        if (t.getAlways() != null && t.getAlways()) {
            tempId = t.getDefaultAlbum();
        } else {
            // 1.从URL中获取的folderId
            if (folderId != null && folderId.intValue() != 0) {
                tempId = folderId;
            }
            // 2.如果从URL中获取不到folderId参数,则偿试使用defaultAlbum指定的folder
            if (tempId == null) {
                tempId = t.getDefaultAlbum();
            }
            // 3.如果从default album中仍找不到，则查找最近一个创建的“公开”的Album
            if (tempId == null) {
                FolderEn lastPublic = FolderSe.findLastPublic();
                if (lastPublic != null) {
                    // 一定要附值给folderId,这可保证下次翻页时不再需要 FolderSe.findLastPublic();
                    folderId = lastPublic.getFolderId();
                    // 直接返回，作为public album不需要执行后面的逻辑
                    return PhotoSe.findByFolderId(pp, folderId);
                }
            }
        }
        if (tempId != null) {
            FolderEn fe = FolderSe.find(tempId);
            if (fe == null) {
                return null;
            }
            if (fe.getSecurity() == Security.PUBLIC || QBlog.getCurrentVisitor().isLogin()) {
                return PhotoSe.findByFolderId(pp, tempId);
            }
            if (fe.getSecurity() == Security.PRIVATE) {
                return null;
            }
            if (fe.getSecurity() == Security.PROTECTED) {
                String fetchCode = QBlog.getParam("fetchCode"); 
                if (fetchCode == null) {
                    return null;
                }
                if (!fetchCode.equals(fe.getFetchCode())) {
                    return null;
                }
                return PhotoSe.findByFolderId(pp, tempId);
            }
        }
        return null;
    }

    // ---- about submit fetch code

    // 提交的fetchCode
    private String submittedFetchCode;

    public String getSubmittedFetchCode() {
        return submittedFetchCode;
    }

    public void setSubmittedFetchCode(String submittedFetchCode) {
        this.submittedFetchCode = submittedFetchCode;
    }

    public String submit() {
        QBlog.redirect("/page/pageId=" + pageId + ",folderId=" + folderId + ",fetchCode=" + submittedFetchCode);
        return null;
    }
}
